#include<bits/stdc++.h>
using namespace std;
const int N = 4e5+5;
int n,f[N],sz1[N],sz2[N];
int find(int x)
{
	if(f[x]==x) return x;
	return f[x] = find(f[x]);
}
void merge(int x,int y)
{
	x = find(x),y = find(y);
	sz2[x]++;
	if(x==y) return;
	f[y] = x,sz1[x]+=sz1[y],sz2[x]+=sz2[y];
}
signed main()
{
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i = 1;i<=2*n;i++)
		f[i] = i,sz1[i] = 1;
	for(int i = 1,u,v;i<=n;i++)
		cin>>u>>v,merge(u,v);
	int ans = 0;
	for(int i = 1;i<=2*n;i++)
		if(find(i)==i)
		{
			ans+=sz1[i];
			if(sz2[i]==sz1[i]-1) ans--;
		}
	cout<<ans;
	return 0;
}

